// pages/step/step.js
var utils=require("../../utils/util.js"); //放在文件开头
 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    openId:0,
    stepList:{},
    syncText:'',
    startDate:'',
    finalDate:'',
    totalsteps:'',
    totalSteps_hidden:0,
    syncText_hidden:0,
    syncData_hidden:0,
    notice_hidden:0,
    noticeText:"",
    trj:'',
    bushu:''
  },

  get3rdSession: function () {
    let that = this
    wx.request({
      url: 'http://127.0.0.1/PHP/index.php',
      data: {
        encryptedData: that.data.encryptedData,
        iv: that.data.iv,
        code: that.data.code,
      },
      method: 'GET', 
      success: function (res) {
        console.log(res);
        // 更新openId的值
        that.setData({
          openId: res.data.openid
        }); 
        //输出今天步数
        var stepList = res.data.sp.stepInfoList;
        // --------------
          // 计算步数总和；在这30天一共走了几步
          var cpuntStep=0;
          
          for(var i in stepList){
            cpuntStep +=stepList[i].step;
            console.log(i+'天步数'+stepList[i].step+',总步数为'+cpuntStep);
          }
          console.log("30天总步数"+cpuntStep);

        // --------------
        // 计算天数
        let array=stepList.length;
        console.log(array)
        for (let i in stepList) { 
          if (i==30) {
            stepList[i].timestamp = utils.js_date_time(stepList[i].timestamp) ;
            // 将当前用户今天步数提取
            // trj:今天日期
            var trj=stepList[i].timestamp;
            // bushu：微信运动步数
            var bushu=stepList[i].step;
          }
          
       }
        // console.log(stepList)
        that.setData({
          stepList: stepList,
          trj:trj,
          bushu:bushu
        }); 
        // console.log(bushu)
      },         
    }) 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.login({
      success: function (res) {
        let code = res.code
        
        that.setData({ code: code })

            wx.getWeRunData({//解密微信运动
              success(res) {
                // console.log(res)
                const wRunEncryptedData = res.encryptedData
                that.setData({
                  encryptedData: wRunEncryptedData
                })
                that.setData({ iv: res.iv })
                that.get3rdSession()//解密请求函数 
              },
              
              fail: function (res) {
                if (res.errMsg == 'getWeRunData:fail auth deny'){
                  wx.showModal({
                    title: '提示',
                    content: '获取微信运动步数，需要开启计步权限',
                    success: function (res) {
                      if (res.confirm) {
                        //跳转去设置
                        wx.openSetting({
                          success: function (res) {
                          }
                        })
                      } else {
                        //不设置
                      }
                    }
                  })
                }
                else {
                  console.log(res.errMsg)
                  wx.showModal({
                    title: '提示',
                    content: '未开通微信运动，请关注“微信运动”公众号后重试',
                    showCancel: false,
                    confirmText: '知道了'
                  })
                  }
              }
              
            })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})